package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSetingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSetingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public void add(List<OrderSetting> list) {
        if(list!=null && list.size()>0){
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否已经进行预约
                long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(countByOrderDate>0){//已经进行过预约，需要修改预约信息
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {//没有预约，插入预约操作
                    orderSettingDao.add(orderSetting);
                }

            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String start = date+"-1";
        String end = date+"-31";
        Map<String,String> map = new HashMap<>();
        map.put("start",start);
        map.put("end",end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);

        List<Map> mapArrayList = new ArrayList<>();
        if(list!=null && list.size()>0) {
            for (OrderSetting orderSetting : list) {
                int date1 = orderSetting.getOrderDate().getDate();
                int number = orderSetting.getNumber();
                int reservations = orderSetting.getReservations();
                Map<String,Object> map1 = new HashMap<>();
                map1.put("date",date1);
                map1.put("number",number);
                map1.put("reservations",reservations);
                mapArrayList.add(map1);
            }
        }
        return mapArrayList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count>0){//已进行过预约设置，需要修改预约信息
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }
}
